<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Users
 *
 * This model represents user authentication data. It operates the following tables:
 * - user account data,
 * - user profiles
 *
 * @package	Tank_auth
 * @author	Ilya Konyukhov (http://konyukhov.com/soft/)
 */
class Invoicemodel extends CI_Model
{
	private $view_name = 'invoices';
	private $table_name = 'invoices_data';
	private $items_view_name = 'invoice_items';
	private $items_table_name = 'invoice_items_data';

	function __construct()
	{
		parent::__construct();
		
	}

	function getAllByOwnerId($id,$sortname='',$sortorder='',$qtype='',$query='',$page='',$rp='')
	{
		$this->db->select('*');
		$this->db->from($this->view_name);
		$this->db->where('owner_id',$id);
		if($sortname != '' && $sortorder != ''){
			$this->db->order_by($sortname,$sortorder);
		}
		if($qtype != '' && $query != ''){
			$this->db->where($qtype,$query);
		}
		if($rp != ''){
			if($page != ''){
				$this->db->limit($rp,($page-1)*$rp);
			} else {
				$this->db->limit($rp);
			}
		}
		$q = $this->db->get();
		log_message('debug','Invoicemodel::getAllByOwnerId('.$id.','.$sortname.','.$sortorder.','.$qtype.','.$query.','.$page.','.$rp.') produced '.$this->db->last_query());
		return $q->result();
	}

	function getCountByOwnerId($id,$qtype='',$query=''){
		$this->db->select('COUNT(*) AS `total`');
		$this->db->from($this->view_name);
		$this->db->where('owner_id',$id);
		if($qtype != '' && $query != ''){
			$this->db->where($qtype,$query);
		}
		
		$q = $this->db->get();
		$q = $q->row(0);
		return $q->total;
	}

	function getAllByClientId($id)
	{
		$this->db->where('client_id',$id);
		$q = $this->db->get($this->table_name);
		return $q->result();
	}

	function getByInvoiceId($id, $processData=true)
	{
		$this->db->where('invoice_id',$id);
		$q = $this->db->get($this->view_name);
		foreach($q->result() as $row){
			if($processData){
				$row=$this->processInvoice($row);
			}
			$row->items = $this->getItemsFor($row->invoice_id);
		}
		return $q->result();
	}

	function getItemsFor($invoice_id, $processData=true){
		$this->db->where('invoice_id',$invoice_id);
		$q = $this->db->get($this->items_view_name);
		foreach($q->result() as $row){
			if($processData){
				$row=$this->processInvoiceItem($row);
			}
		}
		return $q->result();
	}

	function removeItemById($item_id){
		$this->db->where('invoice_item_id',$item_id);
		$this->db->delete($this->items_table_name);
		return ($this->db->affected_rows()>0);
	}

	function processInvoice($invoice){
		if(isset($invoice)){
			$invoice->discount = number_format($invoice->discount, 2, ".","");
			$invoice->taxes = number_format($invoice->taxes, 2, ".","");
			$invoice->paid = number_format($invoice->paid, 2, ".","");
			$invoice->subtotal = number_format($invoice->subtotal, 2, ".","");
			$invoice->balance = number_format($invoice->balance, 2, ".","");
		}
		return $invoice;
	}

	function processInvoiceItem($invoice_item){
		if(isset($invoice_item)){
			$invoice_item->rate = number_format($invoice_item->rate, 2, ".","");
			$invoice_item->quantity = number_format($invoice_item->quantity, 2, ".","");
			$invoice_item->row_total = number_format($invoice_item->row_total, 2, ".","");
		}
		return $invoice_item;
	}

	function insertInvoice($data, $items){
		$this->db->insert($this->table_name, $data);
		$invoice_id = $this->db->insert_id();
		foreach($items as $key=>$item){
			$item['invoice_id']=$invoice_id;
			$items[$key]=$item;
		}
		
		log_message('debug','Invoicemodel::insertInvoice('.print_r($data,true).','.print_r($items,true).') produced the following queries:');
		log_message('debug',$this->db->last_query()." resulted in new invoice_id=".$invoice_id);
		$this->db->insert_batch($this->items_table_name, $items);
		log_message('debug',$this->db->last_query());
		return $invoice_id;
	}

	function updateInvoice($invoice_id, $data, $old_items, $new_items=array()){
		$this->db->update($this->table_name,$data,array('invoice_id'=>$invoice_id));
		foreach($old_items as $key=>$value){
			$this->db->update($this->items_table_name,$value,array('invoice_item_id'=>$key));
		}
		
		foreach($new_items as $key=>$item){
			$item['invoice_id']=$invoice_id;
			$new_items[$key]=$item;
		}
		if($new_items){
			$this->db->insert_batch($this->items_table_name, $new_items);
		}
	}
}
